﻿         //计时器对象
         function Timer(id,times,len) {
			 //是否显示计时时间
			 this.showTime=false;
			 //ele 显示时间的dom=====len 时间数组长度		   
             if(arguments.length>0){
                 if (typeof arguments[0] == "string") {
					     this.ele = arguments[0];
						 this.showTime=true;
						 if(arguments.length==3){
						       this.times=times;
							   this.len=len;
					     }else{
						       this.times=times;
							   this.len=times.length;
						 }
					}else if(arguments[0] instanceof Array){
					           this.times=arguments[0];
							   this.len=this.times.length;
					}else if(typeof arguments[0]=="number"){
					           this.times=arguments[0];
							   this.len=arguments[1];
					}			 
			 }else{
			      alert("计时器参数设置有误！");
			 };
             //timer 计时器
             this.timer = null;
             //count 初始计时
             this.count = 0;
             //计时方法
             this.countType =1;
             //current 记录当前一次计时的指针
             this.current = 0;
         }
         //建立Timer对象的原型对象
         Timer.prototype = {
             begin: function() {
                 this.stop();
                 this.predone();
                 if (this.current < this.len) {
                     //当前倒计时时间
                     this.count = this.times instanceof Array ? this.times[this.current] : this.times;
                     //显示倒计时					
                     this.show();
                     var _this = this;
                     this.timer = setInterval(function() { _this.show(); }, 1000);
                 } else {
                     this.reload();
                 };
             },
             show: function() {
                 if (this.showTime) {
                     if (this.countType == 1) {
                         var h = Math.floor(this.count / 3600);
                         var m = Math.floor((this.count - h * 3600) / 60);
                         var s = this.count - h * 3600 - m * 60;
                         function autoCompZero(num) {
                             if (num < 10) {
                                 return "0" + num.toString();
                             } else {
                                 return num.toString();
                             }
                         }
                         var timeStr = autoCompZero(h)+":"+autoCompZero(m)+":"+autoCompZero(s);
                         document.getElementById(this.ele).innerHTML = timeStr;
                     } else {
                         document.getElementById(this.ele).innerHTML = this.count;
                     }
                 }
                 if (this.count > 0) {
                     this.count--;
                 } else {
                     this.stop();
                     this.current++;
                     this.done();
                 }
             },
             stop: function() {
                 clearInterval(this.timer);
                 this.timer = null;
             },
             reload: function() {
                 this.stop();
                 this.current = 0;
                 this.begin();
             },
             predone: function() {
             },
             done: function() {
             }
         };